import { Component } from '@angular/core';
import { NzUploadFile } from 'ng-zorro-antd/upload';
import { Router } from '@angular/router';
import { HttpService } from '../../service/http.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-filerecord',
  templateUrl: './filerecord.component.html',
  styleUrls: ['./filerecord.component.css'],
})
export class FilerecordComponent {
  uploading = false;
  fileList: NzUploadFile[] = [];
  nzOkDisabled = false;

  constructor(
    private router: Router,
    private httpService: HttpService,
    private modalService: NzMessageService,
    private http: HttpClient,
  ) {}

  ngOnInit() {
    this.getList();
  }

  beforeUpload = (file: NzUploadFile): boolean => {
    this.fileList = this.fileList.concat(file);
    return false;
  };
  public pageInfo: any = {
    pageSize: 10,
    pageNum: 1,
    total: 0,
  };
  public dataList: any = [];
  public showAddFlag = false;
  public showUpdateFlag = false;
  public fileInfo: any = {
    id: 0,
    info: '',
  };
  public allowDownload = false;

  handleUpload(): void {
    this.uploading = true;
  }

  uploadFile() {
    const url = '/rest/file/upload';
    const data = new FormData();
    if (this.fileList.length == 0) {
      this.modalService.error('请上传文件');
      return;
    }
    if (this.fileList.length > 2) {
      this.modalService.error('只能上传一个文件');
      return;
    }
    if (this.fileInfo.info == null || this.fileInfo.info == '') {
      this.modalService.error('文件信息不能为空');
      return;
    }

    const isLt10M = this.fileList[0].size! / 1024 / 1024 < 10;
    if (!isLt10M) {
      this.modalService.error('文件需要小于10M');
      return;
    }
    this.fileList.forEach((file: any) => {
      data.append('file', file);
    });
    this.nzOkDisabled = true;
    this.httpService.post(url, data).subscribe((res: any) => {
      if (res.code == '0000') {
        this.fileInfo.id = res.data.id;
        this.updateFileInfo();
      } else {
        this.fileInfo.id = res.data.id;
        this.modalService.success('上传失败');
        this.nzOkDisabled = false;
      }
    });
  }

  updateFileInfo() {
    const data = {
      id: this.fileInfo.id,
      info: this.fileInfo.info,
    };
    const url = '/rest/file/info';
    return this.httpService.put(url, data).subscribe((res: any) => {
      if (res.code == '0000') {
        this.modalService.success('添加成功！');
        this.showAddFlag = false;
        this.fileList = [];
        this.getList();
        this.nzOkDisabled = false;
      } else {
        this.modalService.error(res.message);
        this.nzOkDisabled = false;
      }
    });
  }

  handleCancel() {
    this.showAddFlag = false;
  }

  update() {}

  showAdd() {
    this.showAddFlag = true;
  }

  deleteConfirm(id: any) {
    const url = '/rest/file/' + id;
    return this.httpService.delete(url).subscribe((res: any) => {
      this.getList();
    });
  }

  getList() {
    const url =
      '/rest/file/pageNum/' +
      this.pageInfo.pageNum +
      '/pageSize/' +
      this.pageInfo.pageSize;
    const data = {};
    return this.httpService.post(url, data).subscribe((res: any) => {
      this.dataList = [];
      this.pageInfo.pageSize = res.page.pageSize;
      this.pageInfo.pageNum = res.page.pageNum;
      this.pageInfo.total = res.page.total;
      this.dataList = res.data;
    });
  }

  cancel() {}

  download(id: any, name: any) {
    this.allowDownload = true;
    const url = this.httpService.backendHost + '/rest/file/download/' + id;
    this.http.get(url, { responseType: 'blob' }).subscribe((data: Blob) => {
      const blob = new Blob([data]);
      const url = window.URL.createObjectURL(blob);
      // 打开新窗口方式进行下载
      // window.open(url);

      // 以动态创建a标签进行下载
      const a = document.createElement('a');
      const fileName = '模板下载';
      a.href = url;
      // a.download = fileName;
      a.download = name;
      a.click();
      window.URL.revokeObjectURL(url);
      this.allowDownload = false;
    });
  }
}
